package com.eimf.saf.base.strutsext;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;

@SuppressWarnings("serial")
public class JsonResult extends StrutsResultSupport
{

    private String contentType;

    @Override
    protected void doExecute(final String arg0, final ActionInvocation arg1) throws Exception
    {
        // 获取响应对象
        final HttpServletResponse response = (HttpServletResponse) arg1.getInvocationContext().get(
                StrutsStatics.HTTP_RESPONSE);

        if (contentType == null)
        {
            contentType = "application/json;charset=UTF-8";
        }
        response.setContentType(contentType);
        // 获取jsonResult并发送响应
        final PrintWriter out = response.getWriter();
        final String jsonResult = (String) arg1.getStack().findValue("jsonResult");
        out.println(jsonResult);
        out.flush();
        out.close();
    }

    public String getContentType()
    {
        return contentType;
    }

    public void setContentType(final String contentType)
    {
        this.contentType = contentType;
    }

}
